﻿<?php
require_once('invoke.php');

////////////////////////////批号函数
 function bat_no_Upd($BAT_NO,$PRD_NO,$WH,$QTY,$QTY1,$CST,$BIL_DD,$VALID_DD,$WH_IO,$ORD_TP='',$PRD_NAME='',$ORD_QTY='',$MAK_DD='',$PRD_MARK=''){
	$myconn = createDB();
    $BAT_CHK="SELECT * FROM PRDT WHERE PRD_NO='".$PRD_NO."' AND CHK_BAT='T'";
    $CURSOR=mssql_query($BAT_CHK,$myconn);
    $NUM=mssql_num_rows($CURSOR);
    if($NUM>0){//////////////批号验证
        //////////拼接语句
        if(!is_numeric($QTY)){/////////////错误回滚接口
            $QTY="NULL";
        }if(!is_numeric($QTY1)||trim($QTY1)==''){/////////////错误回滚接口
            $QTY1="NULL";
        }
        if(!is_numeric($CST)||trim($CST)==''){/////////////错误回滚接口
            $CST="NULL";
        }
        if(!is_numeric($ORD_QTY)||trim($ORD_QTY)==''){
            $ORD_QTY="NULL";
        }
        $CASN_DD=$VALID_DD;
        $MAK_DD="nullif('".trim($MAK_DD)."','')";
        $VALID_DD="nullif('".trim($VALID_DD)."','')";
        $SQL_UPD='UPDATE BAT_REC1 SET ';
        $SQL_ADD="INSERT INTO BAT_REC1(BAT_NO,PRD_NO,PRD_MARK,WH";
        if($WH_IO=="O"){//////////订单是不填写出库入库方向
            $SQL_UPD.="QTY_OUT=ISNULL(QTY_OUT,0)+($QTY),QTY1_OUT=ISNULL(QTY1_OUT,0)+($QTY1),CST=ISNULL(CST,0)+($CST),LST_OTD='$BIL_DD',VALID_DD=$VALID_DD,MADE_DD=$MAK_DD";
            $SQL_ADD.=",QTY_OUT,QTY1_OUT,LST_IND,CST,CLS_ID,VALID_DD,MADE_DD";
        }else if($WH_IO=="I"){
            $SQL_UPD.="QTY_IN=ISNULL(QTY_IN,0)+($QTY),QTY1_IN=ISNULL(QTY1_IN,0)+($QTY1),CST=ISNULL(CST,0)+($CST),LST_IND='$BIL_DD',VALID_DD=$VALID_DD,MADE_DD=$MAK_DD";
            $SQL_ADD.=",QTY_IN,QTY1_IN,LST_IND,CST,CLS_ID,VALID_DD,MADE_DD";
        }
        $SQL_UPD_LINK="";
        if(strtoupper($ORD_TP)=="SO"){////采购引用采购订单
            $SQL_UPD_LINK.=",QTY_ON_ODR=ISNULL(QTY_ON_ODR,0)+($ORD_QTY)";
            $SQL_ADD.=",QTY_ON_ODR";
        }else if(strtoupper($ORD_TP)=="PO"){///销售引用销售订单
            $SQL_UPD_LINK.=",QTY_ON_WAY=ISNULL(QTY_ON_WAY,0)+($ORD_QTY)";
            $SQL_ADD.=",QTY_ON_WAY";
        }else if(strtoupper($ORD_TP)=="LL"){///销售引用销售订单
            $SQL_UPD_LINK.=",QTY_ON_RSV=ISNULL(QTY_ON_RSV,0)+($ORD_QTY)";
            $SQL_ADD.=",QTY_ON_RSV";
        }else if(strtoupper($ORD_TP)=="JK"){///销售引用销售订单
            $SQL_UPD_LINK.=",QTY_ON_PRC=ISNULL(QTY_ON_PRC,0)+($ORD_QTY)";
            $SQL_ADD.=",QTY_ON_PRC";
        }else if(strtoupper($ORD_TP)=="EW"){/////委托代销
            $SQL_UPD_LINK.=",QTY_ON_DGT=ISNULL(QTY_ON_DGT,0)+($ORD_QTY)";
            if($WH_IO==""){////委托核销单
                $SQL_UPD.="CST=ISNULL(CST,0)+($CST),";
                $SQL_ADD.=",CST";
            }
            $SQL_ADD.=",QTY_ON_DGT";
        }else if(strtoupper($ORD_TP)=="TI"){///送检
             $SQL_UPD_LINK.=",QTY_RK=ISNULL(QTY_RK,0)+($ORD_QTY)";
             $SQL_ADD.=",QTY_RK";
        }
        
       $SQL_ADD.=")VALUES('$BAT_NO','$PRD_NO','$PRD_MARK','$WH'";
         if($WH_IO==""&&$ORD_TP!=""){////只存在订单的情况下
            $SQL_UPD.=substr($SQL_UPD_LINK,1);
            if(strtoupper($ORD_TP)=="EW"){////委托代销
              $SQL_ADD.=",$CST";
            }
            $SQL_ADD.=",$ORD_QTY)";   
         }else if($WH_IO!=""&&$ORD_TP!=""){////只存在订单出入库的情况下
            $SQL_UPD.=$SQL_UPD_LINK;
            $SQL_ADD.=",$QTY,$QTY1,'$BIL_DD',$CST,'F',$VALID_DD,$MAK_DD,$ORD_QTY)";
         }else if($WH_IO!=""&&$ORD_TP==""){///只存在出入库的情况
            $SQL_ADD.=",$QTY,$QTY1,'$BIL_DD',$CST,'F',$VALID_DD,$MAK_DD)";
         }
          
        
        $SQL_UPD.=" WHERE BAT_NO='$BAT_NO' AND WH='$WH' AND PRD_NO='$PRD_NO' AND PRD_MARK='$PRD_MARK'";
        $SQL_QUERY="SELECT * FROM BAT_REC1 WHERE BAT_NO='$BAT_NO' AND WH='$WH' AND PRD_NO='$PRD_NO' AND PRD_MARK='$PRD_MARK'";
        $CUR=mssql_query($SQL_QUERY,$myconn);
        if($ROWG = mssql_fetch_array($CUR)){ //echo $SQL_UPD;
            mssql_query($SQL_UPD,$myconn);
        }else{
            mssql_query($SQL_ADD,$myconn);
            if($PRD_NAME==""){
                $PRD_NAME=$PRD_NO;
            }
            $SQL_CAS="SELECT * FROM CASN WHERE CAS_NO='$BAT_NO'";
            $CUR_CAS=mssql_query($SQL_CAS,$myconn);
            $num_cas=mssql_num_rows($CUR_CAS);
             if($num_cas==0){
                  $sql_add_cas="insert into CASN(CAS_NO,NAME)VALUES('$BAT_NO','".$PRD_NAME."(".$CASN_DD.")')";
                   mssql_query($sql_add_cas,$myconn);
             }
        }
        $DEL_QTY_NULL="delete from BAT_REC1 WHERE ISNULL(QTY_IN,0)=0 AND ISNULL(QTY_OUT,0)=0 AND ISNULL(CST,0)=0 AND ISNULL(QTY_LRN,0)=0 AND ISNULL(QTY_BRW,0)=0 AND ISNULL(QTY_ON_WAY,0)=0 AND ISNULL(QTY_ON_PRC,0)=0 AND ISNULL(QTY_ON_RSV,0)=0 AND ISNULL(QTY_ON_ODR,0)=0 AND ISNULL(QTY1_IN,0)=0 AND ISNULL(QTY1_OUT,0)=0 AND ISNULL(QTY1_LRN,0)=0 AND ISNULL(QTY1_BRW,0)=0 AND ISNULL(QTY_RK,0)=0 AND  ISNULL(QTY_SA,0)=0 AND ISNULL(CST_MAKE,0)=0 AND ISNULL(CST_MAN,0)=0 AND ISNULL(CST_PRD,0)=0 AND ISNULL(CST_OUT,0)=0 AND ISNULL(QTY_PA,0)=0 AND ISNULL(QTY_IN_LOCK,0)=0 AND ISNULL(UP_PO,0)=0 AND ISNULL(QTY_ON_TRS,0)=0 AND ISNULL(QTY_ON_DGT,0)=0  AND ISNULL(QTY_OUT_NOSH,0)=0  AND ISNULL(QTY_IN_NOSH,0)=0 AND ISNULL(QTY_ON_BRW_NOSH,0)=0 AND ISNULL(QTY_ON_LRN_NOSH,0)=0 AND ISNULL(QTY_ON_DGT_NOSH,0)=0 AND ISNULL(QTY_ON_TRS_NOSH,0)=0 AND ISNULL(QTY_ON_PRC_NOSH,0)=0  AND ISNULL(QTY_ON_RSV_NOSH,0)=0 AND ISNULL(QTY_ON_WAY_NOSH,0)=0 AND ISNULL(QTY_ON_ODR_NOSH,0)=0 ";
        mssql_query($DEL_QTY_NULL,$myconn);//删除空数量批号
        
        $SQL_CLS_ID_T="UPDATE BAT_REC1 SET CLS_ID='T' WHERE PRD_NO='$PRD_NO' AND WH='$WH' AND PRD_MARK='$PRD_MARK' AND BAT_NO='$BAT_NO' AND ISNULL(QTY_IN,0)-ISNULL(QTY_OUT,0)=0 AND ISNULL(QTY_LRN,0)=0 AND ISNULL(QTY_BRW,0)=0 AND ISNULL(QTY_ON_WAY,0)=0 AND ISNULL(QTY_ON_PRC,0)=0 AND ISNULL(QTY_ON_RSV,0)=0 AND ISNULL(QTY_ON_ODR,0)=0 AND ISNULL(QTY1_LRN,0)=0 AND ISNULL(QTY1_BRW,0)=0 AND ISNULL(QTY_RK,0)=0 AND  ISNULL(QTY_SA,0)=0  AND ISNULL(QTY_PA,0)=0 AND ISNULL(QTY_IN_LOCK,0)=0  AND ISNULL(QTY_ON_TRS,0)=0 AND ISNULL(QTY_ON_DGT,0)=0  AND ISNULL(QTY_OUT_NOSH,0)=0  AND ISNULL(QTY_IN_NOSH,0)=0 AND ISNULL(QTY_ON_BRW_NOSH,0)=0 AND ISNULL(QTY_ON_LRN_NOSH,0)=0 AND ISNULL(QTY_ON_DGT_NOSH,0)=0 AND ISNULL(QTY_ON_TRS_NOSH,0)=0 AND ISNULL(QTY_ON_PRC_NOSH,0)=0  AND ISNULL(QTY_ON_RSV_NOSH,0)=0 AND ISNULL(QTY_ON_WAY_NOSH,0)=0 AND ISNULL(QTY_ON_ODR_NOSH,0)=0";
        mssql_query($SQL_CLS_ID_T,$myconn);//BAT_REC1表中的CLS_ID进行处理
        
        $SQL_CLS_ID_F="UPDATE BAT_REC1 SET CLS_ID='F' WHERE PRD_NO='$PRD_NO' AND WH='$WH' AND PRD_MARK='$PRD_MARK' AND BAT_NO='$BAT_NO' AND ISNULL(QTY_IN,0)-ISNULL(QTY_OUT,0)!=0 "; 
        mssql_query($SQL_CLS_ID_F,$myconn);//BAT_REC1表中的CLS_ID进行处理
    }else{
        prd_no_Upd($PRD_NO,$WH,$QTY,$QTY1,$CST,$ORD_TP,$ORD_QTY,$PRD_MARK);
    }
 }
///////////////////货品插入函数
function prd_no_Upd($PRD_NO,$WH,$QTY,$QTY1,$AMT_CST='',$ORD_TP='',$ORD_QTY='',$PRD_MARK=''){
    $myconn = createDB();
    if(!is_numeric($QTY)||TRIM($QTY)==''){/////////////错误回滚接口
        $QTY="NULL";
    }if(!is_numeric($QTY1)||trim($QTY1)==''){/////////////错误回滚接口
        $QTY1="NULL";
    }
    if(!is_numeric($AMT_CST)||trim($AMT_CST)==''){/////////////错误回滚接口
        $AMT_CST="NULL";
    }
    if(!is_numeric($ORD_QTY)||trim($ORD_QTY)==''){
        $ORD_QTY="NULL";
    }
    $UPD_SQL="UPDATE PRDT1 SET QTY=ISNULL(QTY,0)+ISNULL($QTY,0),QTY1=ISNULL(QTY1,0)+ISNULL($QTY1,0),AMT_CST=ISNULL(AMT_CST,0)+ISNULL($AMT_CST,0)";
    $ADD_SQL="INSERT INTO PRDT1(WH,PRD_NO,PRD_MARK,QTY,QTY1,AMT_CST";
    if(strtoupper($ORD_TP)=="SO"){
        $UPD_SQL.=",QTY_ON_ODR=ISNULL(QTY_ON_ODR,0)+($ORD_QTY)";
        $ADD_SQL.=",QTY_ON_ODR) VALUES('$WH','$PRD_NO','$PRD_MARK',$QTY,$QTY1,$AMT_CST,$ORD_QTY)";
    }else if(strtoupper($ORD_TP)=="PO"){
        $UPD_SQL.=",QTY_ON_WAY=ISNULL(QTY_ON_WAY,0)+($ORD_QTY)";
        $ADD_SQL.=",QTY_ON_WAY) VALUES('$WH','$PRD_NO','$PRD_MARK',$QTY,$QTY1,$AMT_CST,$ORD_QTY)";
    }else if(trim($ORD_TP)=="EW"){
        $UPD_SQL.=",QTY_ON_DGT=ISNULL(QTY_ON_DGT,0)+($ORD_QTY)";
        $ADD_SQL.=",QTY_ON_DGT) VALUES('$WH','$PRD_NO','$PRD_MARK',$QTY,$QTY1,$AMT_CST,$ORD_QTY)";
    }else if(strtoupper(trim($ORD_TP))=="LL"){
        $UPD_SQL.=",QTY_ON_RSV=ISNULL(QTY_ON_RSV,0)+($ORD_QTY)";
        $ADD_SQL.=",QTY_ON_RSV) VALUES('$WH','$PRD_NO','$PRD_MARK',$QTY,$QTY1,$AMT_CST,$ORD_QTY)";
    }else if(strtoupper(trim($ORD_TP))=="JK"){
        $UPD_SQL.=",QTY_ON_PRC=ISNULL(QTY_ON_PRC,0)+($ORD_QTY)";
        $ADD_SQL.=",QTY_ON_PRC) VALUES('$WH','$PRD_NO','$PRD_MARK',$QTY,$QTY1,$AMT_CST,$ORD_QTY)";
    }else if(strtoupper(trim($ORD_TP))=="TI"){
        $UPD_SQL.=",QTY_RK=ISNULL(QTY_RK,0)+($ORD_QTY)";
        $ADD_SQL.=",QTY_RK) VALUES('$WH','$PRD_NO','$PRD_MARK',$QTY,$QTY1,$AMT_CST,$ORD_QTY)";
    }else if(trim($ORD_TP)==""){//
        $ADD_SQL.=") VALUES('$WH','$PRD_NO','$PRD_MARK',$QTY,$QTY1,$AMT_CST)";
    }
    
    
    $UPD_SQL.=" WHERE PRD_NO='$PRD_NO' AND WH='$WH' AND PRD_MARK='$PRD_MARK' ";
    $queryG ="SELECT * FROM PRDT1 WHERE PRD_NO='$PRD_NO' AND WH='$WH' AND PRD_MARK='$PRD_MARK'";
    $CUR=mssql_query($queryG,$myconn);
    if($ROWG = mssql_fetch_array($CUR)){        
        mssql_query($UPD_SQL,$myconn);
    }else{
        mssql_query($ADD_SQL,$myconn);
    }
    
    $DEL_QTY_NULL="delete from PRDT1 WHERE ISNULL(QTY,0)=0 AND ISNULL(QTY1,0)=0 AND ISNULL(AMT_CST,0)=0 AND ISNULL(QTY_LRN,0)=0 AND ISNULL(QTY_BRW,0)=0 AND ISNULL(QTY_ON_WAY,0)=0 AND ISNULL(QTY_ON_PRC,0)=0 AND ISNULL(QTY_ON_RSV,0)=0 AND ISNULL(QTY_ON_ODR,0)=0 AND ISNULL(QTY1_LRN,0)=0 AND ISNULL(QTY1_BRW,0)=0 AND ISNULL(QTY_SA,0)=0 AND ISNULL(QTY_RK,0)=0 AND ISNULL(QTY_PA,0)=0 AND  ISNULL(QTY_LOCK,0)=0 AND ISNULL(QTY_ON_DGT,0)=0 AND ISNULL(QTY_IN_NOSH,0)=0 AND ISNULL(QTY_OUT_NOSH,0)=0 AND ISNULL(QTY_ON_LRN_NOSH,0)=0 AND ISNULL(QTY_ON_BRW_NOSH,0)=0 AND ISNULL(QTY_ON_WAY_NOSH,0)=0 AND ISNULL(QTY_ON_ODR_NOSH,0)=0 AND ISNULL(QTY_ON_RSV_NOSH,0)=0 AND ISNULL(QTY_ON_PRC_NOSH,0)=0  AND ISNULL(QTY_ON_DGT_NOSH,0)=0  AND ISNULL(QTY_ON_TRS_NOSH,0)=0 AND ISNULL(QTY_ON_TRS,0)=0 AND ISNULL(QTY_ON_ODR_NOSH,0)=0 ";
    mssql_query($DEL_QTY_NULL,$myconn);//删除空数量货品
}

//设置编号根据日期
function setDateBillId_JJ($NID,$EDIT_DATE)
{
	$MON=substr($EDIT_DATE,5,2)/1;
	//echo $MON;
	if(strlen($MON)==2)
	{
		if($MON==10)
		{
			$MON="A";
		}
		else if($MON==11)
		{
			$MON="B";
		}
		else if($MON==12)
		{
			$MON="C";
		}
	}
	$myconn = createDB();
	// ECHO $EDIT_DATE;
	 $OS = $NID.substr($EDIT_DATE,3,1).$MON.substr($EDIT_DATE,8,2);
    $OS1 = $OS."NNNN";
    $qrylist =" select * from BILN1 where pat='$OS1' ";
	
	$cursor = mssql_query($qrylist,$myconn);
    $RS = mssql_fetch_array($cursor);
	if($RS==0)
	{
		echo '1';
		$OS_NO=$OS."0001";
		$qrylist  =" insert into BILN1 (sq,PAT) values('2','$OS1') ";
		//msquery($msconn,$qrylist);
		mssql_query($qrylist,$myconn);
	}
	else 
	{
		//$RSS = mssql_fetch_array($cursor);
		$QS= $RS["SQ"];
		$QS++;
		$OS_NO = $OS.substr("0000",0,4-strlen($QS)).$QS;
		$qrylist  =" update BILN1 set sq='$QS' WHERE PAT='$OS1' ";
		//msquery($msconn,$qrylist);
		mssql_query($qrylist,$myconn);
	}
	
	return $OS_NO;
	
}

//根据日期设置编号
function setDateBillId($NID,$EDIT_DATE)
{
	$myconn = createDB();
	/*if(strtotime($init_dd)>=strtotime($EDIT_DATE)){//设置月结后的单号
			 $EDIT_DATE=date("Y-m-d",strtotime($init_dd));
		}*/
	$MON=substr($EDIT_DATE,5,2)/1;
	
	if(strlen($MON)==2)
	{
		if($MON==10)
		{
			$MON="A";
		}
		else if($MON==11)
		{
			$MON="B";
		}
		else if($MON==12)
		{
			$MON="C";
		}
	}
	$OS = $NID.substr($EDIT_DATE,3,1).$MON.substr($EDIT_DATE,8,2);
	$OS1 = $OS."NNNN";
	$qrylist =" select * from BILN1 where pat='$OS1' ";
	//$cursor = msquery($msconn,$qrylist);
	$cursor = mssql_query($qrylist,$myconn);
	$RS = mssql_num_rows($cursor);
	if($RS==0)
	{
		$OS_NO=$OS."0001";
		$qrylist  =" insert into BILN1 (sq,PAT) values('1','$OS1') ";
		//msquery($msconn,$qrylist);
	    mssql_query($qrylist,$myconn);
	}
	else 
	{
		//$RSS = mssql_fetch_array($cursor);
		$QS= $RS["SQ"];
//		$QS++;
		$OS_NO = $OS.substr("0000",0,4-strlen($QS)).$QS;
//		$qrylist  =" update BILN1 set sq='$QS' WHERE PAT='$OS1' ";
//		msquery($msconn,$qrylist);
	}
	
/*	else
	{
		$RSS = mssql_fetch_array($cursor);
		$QS= $RSS["SQ"];
		$QS++;
		$OS_NO = $OS.substr("0000",0,4-strlen($QS)).$QS;
		$qrylist  =" update BILN1 set sq='$QS' WHERE PAT='$OS1' ";
		msquery($msconn,$qrylist);
	}*/
	return $OS_NO;
	
}
 
function SearchDepartmentes(){
	$key = request("key");
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchDepartment($key,$pageIndex,$pageSize,$sortField,$sortOrder);
	
	
	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
}
function SearchDepartmentesCuss(){
	$key = request("key");
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchDepartmentesCuss($key,$pageIndex,$pageSize,$sortField,$sortOrder);
	
	
	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
}
function SaveDepartmentes(){
	$json = request("data");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$myOptionDB->DeleteDepartmente();
	foreach ($rows as $row){
		$myOptionDB->InsertDepartmente($row);
	}  
};
function GetDepartments(){
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetDepartments();
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetArea(){
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetArea();
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetWlComp(){
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetWlComp();
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetReceiverNat(){
	$id = request("id");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetReceiverNat($id);
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetChangedReceiver(){
	$id = request("id");
	$new_tid = request("new_tid");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetChangedReceiver($id,$new_tid);
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetDepartArea(){
	$DEP = request("DEP");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetDepartArea($DEP);
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function SaveDepartmentesArea(){
	$json = request("data");
	$DEP = request("DEP");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$myOptionDB->DeleteDepartmenteArea($DEP);
	foreach ($rows as $row){
		$myOptionDB->InsertDepartmenteArea($row, $DEP);
	}
};

function SearchArea(){
	$key = request("key");
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchArea($key,$pageIndex,$pageSize,$sortField,$sortOrder);
	
	
	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
};
function SearchWlComp(){
	$key = request("key");
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchWlComp($key,$pageIndex,$pageSize,$sortField,$sortOrder);
	
	
	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
};

function SearchBack(){
	$key = request("key");
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchBack($key,$pageIndex,$pageSize,$sortField,$sortOrder);
	
	
	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
};
function SearchStatus(){
	$key = request("key");
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchStatus($key,$pageIndex,$pageSize,$sortField,$sortOrder);
	
	
	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
};
function SaveArea(){
	$json = request("data");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	
	foreach ($rows as $row){
		$id = $row["id"] != null?$row["id"]:"";
		$state = $row["_state"] != null?$row["_state"]:"";
		if($state == "added" || $id == ""){ //新增：id为空，或_state为added
			$myOptionDB->InsertArea($row);
		}
			else if ($state == "removed" || $state == "deleted")
		{
				$myOptionDB->DeleteArea($row);
		}
			else if ($state == "modified" || $state == "")  //更新：_state为空或modified
		{
				$myOptionDB->UpdateArea($row);
		}
	}
};
function SaveIndexOrders(){
	$json = request("data");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	foreach ($rows as $row){
		$id = $row["tid"] != null?$row["tid"]:""; 
		if($id != ""){
			print_r($myOptionDB->SaveIndexOrders($row));
		}
	}
};

function SaveIndexOrdersForms(){
	$json = request("data");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	foreach ($rows as $row){
		$id = $row["tid"] != null?$row["tid"]:""; 
		if($id != ""){
			print_r($myOptionDB->SaveIndexOrdersForms($row));
		}
	}
};
function SaveIndexItemsGrid(){
	$json = request("data");
	$tid = request("tid");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	foreach ($rows as $row){
		$id = $row["num_iid"] != null?$row["num_iid"]:""; 
		$state = $row["_state"] != null?$row["_state"]:"";
		if($state == "added" || $id == ""){ //新增：id为空，或_state为added
			$myOptionDB->InsertIndexItemsGrid($row,$tid);
		}
			else if ($state == "removed" || $state == "deleted")
		{
				$myOptionDB->DeleteIndexItemsGrid($row,$tid);
		}
			else if ($state == "modified" || $state == "")  //更新：_state为空或modified
		{
				$myOptionDB->UpdateIndexItemsGrid($row,$tid);
		}
	}
};
function RemoveIndexItemsGrid(){
	$id = request("id");
	$tid = request("tid");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	print_r($myOptionDB->RemoveIndexItemsGrid($id,$tid));
};
function SaveDEPTBatch(){
	$json = request("data");
	$value = request("value");
	if (!empty($json)){//判断是否为空，防止出错
		//$str = explode(",",$json[0]);
		require_once('MyOptionDB.php');
		$myOptionDB = new MyOptionDB();
		print_r($myOptionDB->SaveDEPTBatch($json,$value));
	}
};
function SaveStatusBatch(){
	$json = request("data");
	$value = request("value");
	if (!empty($json)){//判断是否为空，防止出错
		//$str = explode(",",$json[0]);
		require_once('MyOptionDB.php');
		$myOptionDB = new MyOptionDB();
		print_r($myOptionDB->SaveStatusBatch($json,$value));
	}
};

function batchPrintItemList(){
	$json = request("data");
	$value = request("value");
	if (!empty($json)){//判断是否为空，防止出错
		//$str = explode(",",$json[0]);
		require_once('MyOptionDB.php');
		$myOptionDB = new MyOptionDB();
		print_r($myOptionDB->batchPrintItemList($json,$value));
	}
};

function SaveWlBatch(){
	$json = request("data");
	$value = request("value");
	if (!empty($json)){//判断是否为空，防止出错
		//$str = explode(",",$json[0]);
		require_once('MyOptionDB.php');
		$myOptionDB = new MyOptionDB();
		print_r($myOptionDB->SaveWlBatch($json,$value));
	}
};
function SaveNomoBatch(){
	$json = request("data");
	$value = request("value");
	if (!empty($json)){//判断是否为空，防止出错
		//$str = explode(",",$json[0]);
		require_once('MyOptionDB.php');
		$myOptionDB = new MyOptionDB();
		print_r($myOptionDB->SaveNomoBatch($json,$value));
	}
};
function SaveWlComp(){
	$json = request("data");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	
	foreach ($rows as $row){
		$id = $row["id"] != null?$row["id"]:"";
		$state = $row["_state"] != null?$row["_state"]:"";
		if($state == "added" || $id == ""){ //新增：id为空，或_state为added
			$myOptionDB->InsertWlComp($row);
		}
			else if ($state == "removed" || $state == "deleted")
		{
				$myOptionDB->DeleteWlComp($row);
		}
			else if ($state == "modified" || $state == "")  //更新：_state为空或modified
		{
				$myOptionDB->UpdateWlComp($row);
		}
	}
};
function SaveBack(){
	$json = request("data");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	
	foreach ($rows as $row){
		$id = $row["id"] != null?$row["id"]:"";
		$state = $row["_state"] != null?$row["_state"]:"";
		if($state == "added" || $id == ""){ //新增：id为空，或_state为added
			$myOptionDB->InsertBack($row);
		}
			else if ($state == "removed" || $state == "deleted")
		{
				$myOptionDB->DeleteBack($row);
		}
			else if ($state == "modified" || $state == "")  //更新：_state为空或modified
		{
				$myOptionDB->UpdateBack($row);
		}
	}
};
function SaveStatus(){
	$json = request("data");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	foreach ($rows as $row){
		$id = $row["id"] != null?$row["id"]:"";
		$state = $row["_state"] != null?$row["_state"]:"";
		if($state == "added" || $id == ""){ //新增：id为空，或_state为added
			$myOptionDB->InsertStatus($row);
		}
			else if ($state == "removed" || $state == "deleted")
		{
				$myOptionDB->DeleteStatus($row);
		}
			else if ($state == "modified" || $state == "")  //更新：_state为空或modified
		{
				$myOptionDB->UpdateStatus($row);
		}
	}
};
function SaveStatusColor(){
	$id = request("id");
	$color = request("color");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$myOptionDB->SaveStatusColor($id,$color);
};

function SaveDeptArea(){
	$json = request("data");
	$DEP = request("DEP");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$myOptionDB->InsertDeptArea($json,$DEP);
};
function SaveWlArea(){
	$json = request("data");
	$DEP = request("DEP");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$myOptionDB->InsertWlArea($json,$DEP);
};

function SearchTaobaoOrders(){
	$key = request("key");
	$buyer_nick = request("buyer_nick");
	$date1 = request("date1");
	$date2 = request("date2");
	$deptCombo = request("deptCombo");
	$deptArea = request("deptArea");
	$deptArea = request("deptArea");
	$cbl_status = request("cbl_status");
	$cbl_web = request("cbl_web");
	$order_status = request("order_status");
	
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchTaobaoOrders($key,$buyer_nick, $date1, $date2, $deptCombo, $deptArea, $cbl_status, $order_status,$cbl_web, $pageIndex,$pageSize,$sortField,$sortOrder);

	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
};

function SearchTaobaoHistoryOrders(){
	$key = request("key");
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchTaobaoHistoryOrders($key,$pageIndex,$pageSize);

	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
};
function SearchTaobaoOrdersContact(){
	$key = str_replace(" ","",request("key"));
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchTaobaoOrdersContact($key,$pageIndex,$pageSize,$sortField,$sortOrder);
	
	
	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
};

function SearchTaobaoOrdersByArea(){
	$key = request("key");
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	$buyer_nick = request("buyer_nick");
	$date1 = request("date1");
	$date2 = request("date2");
	$deptCombo = request("deptCombo");
	$deptArea = request("deptArea");
	$deptArea = request("deptArea");
	$cbl_status = request("cbl_status");
	$cbl_web = request("cbl_web");
	$order_status = request("order_status");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchTaobaoOrdersByArea($key,$buyer_nick, $date1, $date2, $deptCombo, $deptArea, $cbl_status, $order_status,$cbl_web, $pageIndex,$pageSize,$sortField,$sortOrder);
	
	
	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
};
function SearchTaobaoOrdersByDept(){
	$key = request("key");
	$deptCombo = request("deptCombo");
	
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchTaobaoOrdersByDept($key,$deptCombo,$pageIndex,$pageSize,$sortField,$sortOrder);
	
	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
};
function GetTaobaoOrder(){
	$id = $_GET["id"];
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetTaobaoOrder($id);
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetTaobaoItems(){
	$id = request("id");
	$pageIndex = request("pageIndex");
    $pageSize = request("pageSize");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetTaobaoItems($id,$pageIndex,$pageSize);
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetTaobaoItemsView(){
	$id = request("id");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetTaobaoItemsView($id);
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetTaobaoItemsHistoryView(){
	$id = request("key");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetTaobaoItemsHistoryView($id);
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetStatusWeb(){
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetStatusWeb();
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetStorage(){
	$oid = request("oid");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetStorage($oid);
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function GetStatus(){
	$out_status_id = request("oid");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetStatus($out_status_id);
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function SaveContactOrders(){
	$json = request("data");
	if (!empty($json)){//判断是否为空，防止出错
		//$str = explode(",",$json[0]);
		require_once('MyOptionDB.php');
		$myOptionDB = new MyOptionDB();
		$myOptionDB->SaveContactOrders($json);
	}
};
function SaveContactOrdersAll(){
	$json = request("data");
	if (!empty($json)){//判断是否为空，防止出错
		//$str = explode(",",$json[0]);
		require_once('MyOptionDB.php');
		$myOptionDB = new MyOptionDB();
		
		print_r($myOptionDB->SaveContactOrdersAll($json));
	}
};
function SaveSplitOrdersAll(){
	$id = request("id");
	$json = request("data");
	if (!empty($json)){//判断是否为空，防止出错
		$str = explode(",",$json[0]);
		require_once('MyOptionDB.php');
		$myOptionDB = new MyOptionDB();
		
		print_r($myOptionDB->SaveSplitOrdersAll($json,$id));
	}
};

function SavefpOrdersIsComponte(){
	$json = request("data");
	if (!empty($json)){//判断是否为空，防止出错
		require_once('MyOptionDB.php');
		$myOptionDB = new MyOptionDB();
		
		print_r($myOptionDB->SavefpOrdersIsComponte($json));
	}
};
function SavefpOrdersIsComponteAll(){
	$json = request("data");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	
	print_r($myOptionDB->SavefpOrdersIsComponteAll($json));
};
function SaveSplitOrders(){
	$json = request("data");
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$myOptionDB->SaveSplitOrders($json);
};
function SaveOrders(){
	$json = request("data");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	foreach ($rows as $row){
		$id = $row["tid"] != null?$row["tid"]:"";
		$state = $row["_state"] != null?$row["_state"]:"";
		if ($state == "modified" || $state == "")  //更新：_state为空或modified
		{
				$myOptionDB->UpdateOrders($row);
		}
	}  
};
function SaveContactedOrder(){
	$json = request("data");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	foreach ($rows as $row){
		//$myOptionDB->SaveContactedOrder($row, $DEP);
		print_r($myOptionDB->SaveContactedOrder($row, $DEP));
	}
};
function GetSplitTaobaoItemsView(){
	$id = request("id");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$data = $myOptionDB->GetSplitTaobaoItemsView($id);
	$json = json_encode(gbk2utf8($data));
	print_r($json);
};
function SearchTBJDItems(){
	$key = request("key");
	$pageIndex = request("pageIndex");
	$pageSize = request("pageSize");
	
	$sortField = request("sortField");
    $sortOrder = request("sortOrder");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->SearchTBJDItems($key,$pageIndex,$pageSize,$sortField,$sortOrder);
	
	
	ob_end_clean();
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
}
function GetSendOrdersList(){
	$data = request("data");
	
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$resultData = $myOptionDB->GetSendOrdersList($data);
	$json = json_encode(gbk2utf8($resultData));
	print_r($json);
};
function sendOrdersMethod(){
	$json = request("data");
	$loginUserId = request("loginUserId");
	$rows = php_json_decode($json);
	require_once('MyOptionDB.php');
	$myOptionDB = new MyOptionDB();
	$myconn = createDB();
	//银行账号的取得
	$query1 = "select * from ds_back where [ds_type] = 'TB'";
	$cursor1= mssql_query($query1,$myconn);
	while($ROW1=mssql_fetch_array($cursor1))
	{
		$BACC_NO=$ROW1["ds_back_cd"];
		break;
	}
	foreach ($rows as $row){
		//================================
		$tid = $row["tid"];
		$isCus = $row["isCus"];
		$diansType = $row["orderType"];
		$DEP = $row["DEP"];
		$ddlCompany = $row["WL_ID"];
		$txtOutSid = $row["SEND_NO"];
		$taobao_buyer_nick = $row["buyer_nick"];
		$Story_ID = $row["MY_WH"];
		$pay_time = $row["pay_time"]==""?'null':"cast('".$row["pay_time"]."' as datetime)";
		$hidPayment = $row["payment"];
		$s_packageNum = request("s_packageNum");//******* not use
		if($row["rblOrderType"] == "" || $row["rblOrderType"] == null){
			$rblOrderType = "delivery_needed";
		} else {
			$rblOrderType = $row["rblOrderType"];
		}
		
		$SessionKey = true;
		$client = new SoapClient("http://192.168.1.23/TaobaoWebService.asmx?WSDL",array ('encoding' => 'gb2312'));
		//编码问题的解决
		$client->soap_defencoding = 'gb2312';
		$client->decode_utf8 = false;
		$client->xml_encoding = 'gb2312';
		
		//发货处理
		if($tid != "")
		{
			if($diansType == "JD")
			{
				//参数这样传递 先包装一下
				$param = array('tid'=>$tid,'rblOrderType'=> $rblOrderType, 'ddlCompany'=>$ddlCompany, 'txtOutSid'=>$txtOutSid, 'packageNum'=>$packageNum);
			}
			else
			{
				//淘宝发货
				//参数这样传递 先包装一下
				$param = array('tid'=>$tid,'rblOrderType'=> 'delivery_needed', 'ddlCompany'=>$ddlCompany, 'txtOutSid'=>$txtOutSid, 'packageNum'=>'0');
			}
			//调用必须用__soapCall
			//$p = $client->__soapCall('send',array('parameters' => $param));
			if(strpos($p->sendResult,'0;errMsg') == false)
			{
				$SessionKey = false;
				//print_r($p->sendResult);
				print_r(json_encode(gbk2utf8("发货失败！请重试或与管理员联系！")));
				return;
			}
			else
			{
				//发货号
				$REM = "快递公司：" .$ddlCompany .";快递单号：".$txtOutSid;
			
				$CUR_DATE=date("Y-m-d",time());
				$CUR_TIME=date("Y-m-d H:i:s",time());
				if($DIA_DATE=="")
				   $DIA_DATE=$CUR_DATE;
				   
				//系统时间
				$SARP_DD=date("Y-m-d",time());
				//单号
				$BIL_ID=setDateBillId_JJ("SA",date('Y-m-d'));
				//订单类型
				$orderType = $diansType;
				//淘宝账号
				$buyer_customer = $taobao_buyer_nick;
				//淘宝单号
				$taobao_tid = $tid;
				//淘宝发货总金额
				$payment=$hidPayment;
				//操作员
				$login_uid= $loginUserId;
				//立账单号:自动生成，和下面的表同时插入
				$ARP_NO = setDateBillId("RT",date('Y-m-d'));
				$CLS_DATE = date("Y-m-d H:i:s");//终审日期
				
				//插入到主表中MF_PSS
				$query="INSERT INTO MF_PSS (ApprovalStatus,PS_ID,PS_NO,PS_DD,CHK_DD,BAT_NO,CUS_NO,
						DEP,TAX_ID,OS_NO,SEND_MTH,ZHANG_ID,
						SAL_NO,ARP_NO,
						REM,PAY_MTH,PAY_DAYS,CHK_DAYS,INT_DAYS,
						PAY_REM,USR,CHK_MAN,PRT_SW,DIS_CNT,CLS_DATE,
						CK_CLS_ID,LZ_CLS_ID,FC_ID,EFF_DD,RECORD_DD,CLS_ID,CREATE_DD,PAY_DD,EXC_RTO,QC_YN)
						VALUES('1','SA','$BIL_ID',$pay_time,$pay_time,null,'".iconv("utf-8",'gb2312',$buyer_customer)."',
						'$DEP','1','$taobao_tid','1','1',
						'$login_uid','$ARP_NO',
						'$REM','','1','1','30',
						'','$login_uid','$login_uid','N','0',getdate(),
						'F','F','F',getdate(),getdate(),'',getdate(),null,'1',null)";
				//echo $query;
				mssql_query($query,$myconn);
				//采购入库单号
				$BIL_CG_ID=setDateBillId_JJ("PC",date('Y-m-d'));
				if($isCus == "1"){
					//厂商供应商时，采购入库
					$query="INSERT INTO MF_PSS (ApprovalStatus,PS_ID,PS_NO,PS_DD,CHK_DD,BAT_NO,CUS_NO,
							DEP,TAX_ID,OS_NO,SEND_MTH,ZHANG_ID,
							SAL_NO,ARP_NO,
							REM,PAY_MTH,PAY_DAYS,CHK_DAYS,INT_DAYS,
							PAY_REM,USR,CHK_MAN,PRT_SW,DIS_CNT,CLS_DATE,
							CK_CLS_ID,LZ_CLS_ID,FC_ID,EFF_DD,RECORD_DD,CLS_ID,CREATE_DD,PAY_DD,EXC_RTO,QC_YN,SPZT)
							VALUES('1','SA','$BIL_CG_ID',$pay_time,$pay_time,null,'".iconv("utf-8",'gb2312',$buyer_customer)."',
							'$DEP','1','$taobao_tid','1','1',
							'$login_uid','$ARP_NO',
							'$REM','','1','1','30',
							'','$login_uid','$login_uid','N','0',getdate(),
							'F','F','F',getdate(),getdate(),'',getdate(),null,'1',null,0)";
					
					mssql_query($query,$myconn);
				}
				/*表头信息插入结束*/
				//淘宝客户号,先在M3查，没有插入
				$query31 = "select * from cust where CUS_NO='$buyer_customer'";
				//$cursor31= msquery($msconn,$query31);
				$cursor31=mssql_query($query31,$myconn);
				while($ROW31=mssql_fetch_array($cursor31))
				{
					$hasCus = "1";
					break;
				}
				if($hasCus!="1"){
					//添加淘宝客户
					$ins_t_cust="insert into cust(CUS_NO,NAME,OBJ_ID,CLS_MTH,CLS_DD,MM_END,CHK_DD,SAL,CREATE_DD,EFF_DD,
								USR,CHK_MAN,CLS_DATE,UPD_DD)
								values('".iconv("utf-8",'gb2312',$buyer_customer)."','".iconv("utf-8",'gb2312',$buyer_customer)."','1',1,1,30,30,'$login_uid',getdate(),getdate(),
								'$login_uid','$login_uid',getdate(),getdate())";
					$ins_t_cust = iconv("utf-8",'gb2312',$ins_t_cust);
					//msquery($msconn,$ins_t_cust); 
					mssql_query($ins_t_cust,$myconn);
				}
				
				/*循环表身开始*/
				//$query1 = "select * from dbo.TB_Order where tid='$taobao_tid'";
				$query1 = "select * from View_TBJD_Items where tid='$taobao_tid' and orderType ='$orderType'";
			    //echo $query1;
				$NOTIFY_COUNT1=0;
				//$cursor1= msquery($msconn,$query1);
				$cursor1= mssql_query($query1,$myconn);
				while($ROW1=mssql_fetch_array($cursor1))
				{
					$NOTIFY_COUNT1++;
					//商品名称
					$M3_PRD_NAME = "";
					//商品单位
					$M3_PRD_UNIT = "";
					//选择：仓库编号
					$WH1 = $Story_ID;
					//淘宝商品ID
					$s_num_iid=$ROW1["num_iid"];
					
					//淘宝商品名称,先在M3查商品名称，没有再从淘宝查
					$query2 = "select NAME,UT from prdt where PRD_NO='$s_num_iid'";
					//$cursor2= msquery($msconn,$query2);
					$cursor2= mssql_query($query2,$myconn);
					while($ROW2=mssql_fetch_array($cursor2))
					{
					   $M3_PRD_NAME=$ROW2["NAME"];
					   $M3_PRD_UNIT=$ROW2["UT"];
					   break;
					}
					if($M3_PRD_NAME == "")
					{
						$s_title=$ROW1["title"];
						//添加商品
						$ins_t_prdt="insert into prdt(PRD_NO,NAME,SPC,UT,KND,WH,CHK_NUM,CHK_TAX,PRD_DD,LOCK_STOCK_QTY,
									EFF_DD,USR,CHK_MAN,CLS_DATE,UPD_DD)
									VALUES('$s_num_iid','".iconv("utf-8",'gb2312',$s_title)."',NULL,'".iconv("utf-8",'gb2312','件')."','1','0000','F','F',getdate(),'F',
									getdate(),'$login_uid','$login_uid',getdate(),getdate())";
						//msquery($msconn,$ins_t_prdt); 
						mssql_query($ins_t_prdt,$myconn);
					}
					else
					{
						$s_title=$M3_PRD_NAME;
					}
					//淘宝商品数量
					$s_num=$ROW1["num"];
					//淘宝单价
					$s_price=$ROW1["price"];
					//淘宝单价*数量
					$s_payment=$ROW1["total_fee"];
					//批号
					$s_pihao=$ROW1["pihao"];
					//淘宝商品购买备注
					$s_sku_properties_name=$ROW1["sku_properties_name"];
					//插入语句
					$query="INSERT INTO TF_PSS(PS_ID,PS_NO,ITM,PS_DD,BAT_NO,
							OS_NO,PRD_NO,PRD_NAME,PRD_MARK,
							UNIT,QTY,QTY1,UP,AMTN_NET,AMTN,AMT,
							TAX,DIS_CNT,AMTN_EP,VALID_DD,AMTN_COM,EST_DD,TAX_RTO,CST_STD,EST_ITM,
							OS_ID,QTY_PS,PRE_ITM,QTY_LOSS,SEQ_ITM,UP_END,EFF_DD,SEFF_DATE,WH,REM,
							OTH_ITM,UT1,UT) VALUES(
							'SA','$BIL_ID','$NOTIFY_COUNT1',getdate(),'$s_pihao',
							'$taobao_tid','$s_num_iid','$s_title',NULL,
							'$M3_PRD_UNIT','$s_num','0','$s_price','$s_payment','$s_payment','$s_payment',
							'0','0',NULL,getdate(),'0',getdate(),'0','0','$NOTIFY_COUNT1',
							'SO','$s_num','$NOTIFY_COUNT1','0',NULL,'$s_price',getdate(),getdate(),'$WH1','$s_sku_properties_name',
							NULL,'','".iconv("utf-8",'gb2312','件')."')";
					//echo $query;
					mssql_query($query,$myconn);
					//采购入库
					if($isCus == "1"){
						$query="INSERT INTO TF_PSS(PS_ID,PS_NO,ITM,PS_DD,BAT_NO,
								OS_NO,PRD_NO,PRD_NAME,PRD_MARK,
								UNIT,QTY,QTY1,UP,AMTN_NET,AMTN,AMT,
								TAX,DIS_CNT,AMTN_EP,VALID_DD,AMTN_COM,EST_DD,TAX_RTO,CST_STD,EST_ITM,
								OS_ID,QTY_PS,PRE_ITM,QTY_LOSS,SEQ_ITM,UP_END,EFF_DD,SEFF_DATE,WH,REM,
								OTH_ITM,UT1,UT) VALUES(
								'SA','$BIL_CG_ID','$NOTIFY_COUNT1',getdate(),'$s_pihao',
								'$taobao_tid','$s_num_iid','$s_title',NULL,
								'$M3_PRD_UNIT','$s_num','0','$s_price','$s_payment','$s_payment','$s_payment',
								'0','0',NULL,getdate(),'0',getdate(),'0','0','$NOTIFY_COUNT1',
								'SO','$s_num','$NOTIFY_COUNT1','0',NULL,'$s_price',getdate(),getdate(),'$WH1','".iconv("utf-8",'gb2312',$s_sku_properties_name)."',
								NULL,'','".iconv("utf-8",'gb2312','件')."')";
	
						mssql_query($query,$myconn);
					}
							
					//库存表
					//有批号：
					if($s_pihao!=null && $s_pihao!= "") {
						bat_no_Upd($s_pihao,$s_num_iid,$WH1,$s_num,0,$s_payment,$CUR_DATE,NULL,'O','SO');
					} else {
						//没批号：
						prd_no_Upd($s_pihao,$WH1,$s_num,0,$s_payment,'SO');
					}
					//采购入库
					if($isCus == "1"){
						if($s_pihao!=null && $s_pihao!= "") {
							bat_no_Upd($s_pihao,$s_num_iid,$WH1,$s_num,0,$s_payment,$CUR_DATE,NULL,'O','PO');
						} else {
							//没批号：
							prd_no_Upd($s_pihao,$WH1,$s_num,0,$s_payment,'PO');
						}
					}
					
					//应收/应付表MF_ARP
					

					////$success_seq=@msquery($msconn,$query);
//					$success_seq=mssql_query($query,$myconn);
//					//修改库存表
//					//##'增量增加、、减，负数' key:仓库ID，商品编号
//					$queryG ="SELECT * FROM PRDT1 WHERE PRD_NO='$s_num_iid' AND WH='$WH1'";
//					//$cursorG = @msquery($msconn,$queryG);
//					$cursorG=mssql_query($queryG,$myconn);
//					if($ROWG = mssql_fetch_array($cursorG)){
//						$query= " UPDATE PRDT1 SET QTY=ISNULL(QTY,0)-$s_num,QTY1=ISNULL(QTY1,0)-$s_num,
//								  QTY_ON_ODR=ISNULL(QTY_ON_ODR,0)-$s_num WHERE PRD_NO='$s_num_iid' AND WH='$WH1' ";
//						//@msquery($msconn,$queryG);
//						mssql_query($queryG,$myconn);
//					}else{
//						$query= "INSERT INTO PRDT1(WH,PRD_NO,PRD_MARK,QTY,QTY1,AMT_CST)
//								VALUES('$WH1','$s_num_iid','',-$s_num,-$s_num,0)";
//						//@msquery($msconn,$queryG);
//						mssql_query($queryG,$myconn);
//					}
				}
			
			//修改SARP//淘宝账号
			//##KEy 年，月，客户ID
			if($payment==""){
				$payment=0;
				}
			if($s_payment==""){
			$s_payment=0;
				}
			$queryGG ="select * from SARP WHERE YEARS=YEAR('".$SARP_DD."') AND 
					  MONTHS=MONTH('".$SARP_DD."') AND CUS_NO='".iconv("utf-8",'gb2312',$buyer_customer)."' AND ARP_ID=1";
					//$cursorGG = @msquery($msconn,$queryGG);
					$cursorGG =	mssql_query($queryGG,$myconn);
					if($ROWGG = mssql_fetch_array($cursorGG)){
						//AMTN_SO=ISNULL(AMTN_SO,0)-ISNULL($payment,0)
						$SSS="UPDATE SARP SET AMTN_CLS=ISNULL(AMTN_CLS,0)+ISNULL('$s_payment',0) WHERE YEARS=YEAR('".$SARP_DD."') AND MONTHS=MONTH('".$SARP_DD."') AND CUS_NO='".iconv("utf-8",'gb2312',$buyer_customer)."' AND ARP_ID=1";						
					}else{
						$SSS="INSERT INTO SARP(ARP_ID,YEARS,CUS_NO,MONTHS,AMTN_ARP,AMTN_CLS,AMTN_FAX,AMTN_CHK,AMT_ARP,IDX_NO,AMTN_SO)
							  VALUES('1',YEAR('".$SARP_DD."'),'".iconv("utf-8",'gb2312',$buyer_customer)."',MONTH('".$SARP_DD."'),'0',0,0,0,0,'',ISNULL($s_payment,0))";	
					} 
					//@msquery($msconn,$SSS);
					mssql_query($SSS,$myconn);
								
			//##MF_ARP应收应付款明细
			$MF_ARP_NO = setDateBillId_JJ("AR",date('Y-m-d'));
			$MF_ARP_SQL="INSERT INTO MF_ARP(ARP_ID,OPN_ID,ARP_NO,BIL_NO,
						 BIL_DD,CUS_NO,SAL_NO,DEP, AMTN,AMT,
						 AMTN_NET,TAX,AMTN_RSV,EXC_RTO,USR_NO,CHECK_MAN,BIL_ID,PAY_REM,LZ_DD,PAY_DD,BAT_NO,INV_NO,CUR_ID) 
						 VALUES ('1','2','$MF_ARP_NO','$BIL_ID',
						 getdate(),'".iconv("utf-8",'gb2312',$buyer_customer)."','$login_uid','0000',ISNULL($s_payment,0),'0',
						 ISNULL($s_payment,0),'0',NULL,'1','".iconv("utf-8",'gb2312',$buyer_customer)."','".iconv("utf-8",'gb2312',$buyer_customer)."','SA','',getdate(),getdate(),'','','')";
			//@msquery($msconn,$MF_ARP_SQL);
			//echo $MF_ARP_SQL;
			mssql_query($MF_ARP_SQL,$myconn);
			
			//采购入库
			if($isCus == "1"){
				$MF_ARP_SQL="INSERT INTO MF_ARP(ARP_ID,OPN_ID,ARP_NO,BIL_NO,
							 BIL_DD,CUS_NO,SAL_NO,DEP, AMTN,AMT,
							 AMTN_NET,TAX,AMTN_RSV,EXC_RTO,USR_NO,CHECK_MAN,BIL_ID,PAY_REM,LZ_DD,PAY_DD,BAT_NO,INV_NO,CUR_ID) 
							 VALUES ('2','2','$MF_ARP_NO','$BIL_ID',
							 getdate(),'".iconv("utf-8",'gb2312',$buyer_customer)."','$login_uid','0000',ISNULL($s_payment,0),'0',
							 ISNULL($s_payment,0),'0',NULL,'1','".iconv("utf-8",'gb2312',$buyer_customer)."','".iconv("utf-8",'gb2312',$buyer_customer)."','SA','',getdate(),getdate(),'','','')";
				//@msquery($msconn,$MF_ARP_SQL);
				mssql_query($MF_ARP_SQL,$myconn);
			}
			//*****************************即收
			//系统时间
			$PS_DD=date("Y-m-d",time());
			//BACC_TYPE=1，银行卡
			$AMTN_BB=$hidPayment;
			$AMT_BB=$hidPayment;
			$AMT=$hidPayment;
			$PAY_AMTN=$hidPayment;
			$AMTN_BC="NULL";
			$AMT_BC="NULL";
			$CACC_NO="";
			$MBACC_NO=$BACC_NO;
			$BC_NO="";
			$BB_NO1=setDateBillId_JJ("BT",$PS_DD);	/////账户变动
			$queryG ="select * from SBAC WHERE YEARS=YEAR('".$PS_DD."') AND BACC_NO='$BACC_NO' AND DEP='$DEP' AND MONTHS=MONTH('".$PS_DD."')";
			$cursorG = mssql_query($queryG,$myconn);
			if($ROWG = mssql_fetch_array($cursorG)){
			  mssql_query("UPDATE SBAC SET AMTN=AMTN-$PAY_AMTN,AMT=isnull(AMT,0)-isnull($AMT,0) WHERE YEARS=YEAR('".$PS_DD."') AND BACC_NO='$BACC_NO' AND DEP='$DEP' AND MONTHS=MONTH('".$PS_DD."')",$myconn);
			}else{
			  mssql_query("INSERT INTO SBAC (YEARS,BACC_NO,DEP,MONTHS,OPN_ID,AMTN,AMT) VALUES(YEAR('".$PS_DD."'),'$BACC_NO','$DEP',MONTH('".$PS_DD."'),'F',-$PAY_AMTN,-$AMT)",$myconn);
			}
			//***********************************
    		$RP_NO=setDateBillId_JJ("PT",$PS_DD);
			$BAC_BIL_NO="RT".$RP_NO;
			$EXC_RTO = 1;
			  $query="insert into MF_BAC(BB_ID,BB_NO,BB_DD,BACC_NO,DEP,BIL_NO,PAY_MAN,EXC_RTO,AMTN,AMT,USR,CHK_MAN,OPN_ID,CLS_DATE,
			  EFF_DD,RECORD_DD,CUR_ID,ACC_NO,ZP_NO,REM) 
			  values ('BT','$BB_NO1',$pay_time,'$BACC_NO','$DEP','$BAC_BIL_NO','$login_uid','1',-$payment,0,'$login_uid','$login_uid',
			  'F',$pay_time,$pay_time,GETDATE(),'$CUR_ID','','',NULL)";
			  //echo $query;
			  mssql_query($query,$myconn);
			 
			  $query="insert into TF_BAC (BB_ID,BB_NO,ITM,BB_DD,ADD_ID,EXC_RTO,AMTN,AMT,DEP,CUS_NO,PRE_ITM,CUR_ID,INV_NO,REM,ZP_NO) 
			  values  ('BT','$BB_NO1','1','$PS_DD','-','1',$payment,0,'$DEP','$CUS_NO',1,NULL,'',NULL,'')";
			  //echo $query;
			  mssql_query($query,$myconn);
			  
		   $query="insert into MF_MON (RP_ID,RP_NO,RP_DD,USR_NO,DEP,AMTN,REM,BIL_ID,BIL_NO,CHK_MAN,CLS_DATE,EFF_DD,AMTN_ARP,AMTN_REST) 
        	values (1,'$BAC_BIL_NO','$PS_DD','$login_uid','$DEP','$payment',NULL,'SA','$BIL_ID','$login_uid','$CLS_DATE','$CLS_DATE',0,$PAY_AMTN)";
			//echo $query;
        	mssql_query($query,$myconn);
			$AR="AR".$RP_NO;
           $S2="insert into TC_MON(RP_ID,RP_NO,ITM,ITM2,ARP_NO,CUR_ID,AMTN_CLS,AMT_CLS,ARP_OPN_ID,RP_DD,CUS_NO,SAL_NO,DEP,BIL_DD,PRE_ITM,EXC_RTO,OS_NO,AMT_CLS_H)         values('1','$BAC_BIL_NO','1',1,'$AR',NULL,$payment,0,'2','$PS_DD','$buyer_customer',NULL,'$DEP',GETDATE(),'1','1','$taobao_tid',0)";
		   //echo $S2;
           	mssql_query($S2,$myconn);
            
          // $CLS_ID=$TOTLE==$PAY_AMTN?"T":"F";////付所有的款后
        $ins_a_cbhs="insert into tf_mon(RP_ID,RP_NO,ITM,IRP_ID,CLS_ID,RP_DD,CUS_NO,INCLUDESON,AMTN_BC,
            AMTN_BB,AMT_BC,AMT_BB,BC_NO,CACC_NO,BB_NO,BACC_NO,USR,CHK_MAN,CLS_DATE,BZ_ID,EFF_DD,RECORD_DD,EXC_RTO,DEP,AMTN_CLS,AMT_CLS,CUR_ID,PRE_ITM)
             values('1','$RP_NO','1','F','T','$PS_DD','".iconv("utf-8",'gb2312',$buyer_customer)."','F',$AMTN_BC,$AMTN_BB,$AMT_BC,$AMT_BB,'$BC_NO'
,'$CACC_NO','$BB_NO1','$MBACC_NO','$login_uid','$login_uid','$CLS_DATE','F',NULL,GETDATE(),'$EXC_RTO','$DEP',$payment,0,NULL,'1')";
//echo $ins_a_cbhs;
           	mssql_query($ins_a_cbhs,$myconn);
			}
		}
	} 
	//======================================
	$myOptionDB->sendOrdersMethod($row);
	//======================================
	print_r(json_encode(gbk2utf8("发货成功！")));
}
		
function GetIncrementTradeListTwoDaysResult(){
	$loginUserId=request("loginUserId");
	$SEND_TIME_MIN = date("Y-m-d",time());
		$SEND_TIME_MAX = date("Y-m-d",time());
		$client = new SoapClient("http://192.168.1.23/TaobaoWebService.asmx?WSDL",array ('encoding' => 'gb2312'));
		//编码问题的解决
		$client->soap_defencoding = 'gb2312';
		$client->decode_utf8 = false;
		$client->xml_encoding = 'gb2312';
		//重新调用接口
		$param = array('username'=>$loginUserId,'psw'=>'123');
		//调用必须用__soapCall
		$p = $client->__soapCall('GetIncrementTradeListTwoDays',array('parameters' => $param));
		//print_r($p->GetIncrementTradeListResult);  //这里先输出一下变量$p，看看是什么类型。
		if(strpos($p->GetIncrementTradeListTwoDaysResult,'0;errMsg') === false)
		{
			$SessionKey = false;
			print_r($p->GetIncrementTradeListTwoDaysResul);
		}
		print_r("0");
}
?>